package com.source.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @Author ZZF
 * @Time 2021/03/18 15:06
 */
@Component
public class EmailUtils {
    @Resource
    private JavaMailSenderImpl javaMailSender;
    /**
     * 发件人
     */
    @Value("${spring.mail.username}")
    private String emailFrom;

    public void sendEmail(String emailTo, String code) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,false);
        mimeMessageHelper.setSubject("光禽溯源系统登录邮箱验证");
        mimeMessageHelper.setText("验证码" + code);
        mimeMessageHelper.setTo(emailTo);
        mimeMessageHelper.setFrom(new InternetAddress("scorpius" + "<" + emailFrom + ">"));
        javaMailSender.send(mimeMessage);
    }
}
